<?php $this->load->view('layout/header'); ?>
<div class="container-fluid tourism-goods-add-step2">
    <div class="row-fluid">
        <div class="span12">
            <h3 class="page-title">商城管理 <small> 商品复制</small></h3>
            <?php echo breadcrumb(array('tourismgoods/grid' => '商城管理', "tourismgoods/edit" => '复制商品')); ?>
        </div>
    </div>
    <div class="alert alert-error" style="display:none;">
        <button data-dismiss="alert" class="close"></button>
        <a href="javascript:;" class="glyphicons no-js remove_2"><i></i><p></p></a>
    </div>
    <div class="row-fluid">
        <div class="span12">
            <div class="portlet box green">
                <div class="portlet-title">
                    <div class="caption"><i class="icon-plus-sign"></i>复制商品</div>
                    <div class="tools">
                        <a class="collapse" href="javascript:;"></a>
                        <a class="remove" href="javascript:;"></a>
                    </div>
                </div>
                <div class="portlet-body form">
                    <form class="form-horizontal tourismgoods-form" action="<?php echo base_url('tourismgoods/addPost') ?>" method="post" enctype="multipart/form-data">
                        <div class="alert alert-success">商品基本信息</div>
                        <div class="control-group add-supplieruid-html">
                            <label class="control-label"><em>* </em>供应商</label>
                            <div class="controls">
                                <input type="text" name="supplierUid" value="<?php echo $result->uid; ?>" class="m-wrap span8 required">
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">商品类别</label>
                            <div class="controls">
                                <input type="hidden" name="status" value="1"/> <!-- 1代表分润 -->
                                <input type="hidden" name="category_id" value="<?php echo $result->category_id; ?>"/>
                                <input type="text" name="category_name" value="<?php echo $category; ?>" class="m-wrap span8 required" readonly="readonly"/>
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label"><em>* </em>商品名称</label>
                            <div class="controls">
                                <input type="text" name="goods_name" value="<?php echo $result->goods_name; ?>" class="m-wrap span8 required" placeholder="商品名称">
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">关键词</label>
                            <div class="controls">
                                <input type="text" name="keyword" value="<?php echo $result->keyword; ?>" class="m-wrap span8" placeholder="关键词以逗号分隔 例如：喜糖，糖">
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label"><em>* </em>商品简介</label>
                            <div class="controls">
                                <textarea name="goods_brief" rows="3" class="m-wrap span8 required"><?php echo $result->goods_brief; ?></textarea>
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label"><em>* </em>pc商品详情</label>
                            <div class="controls">
                                <textarea name="goods_desc" rows="3" class="textarea-multipart-edit m-wrap span8 required"><?php echo $result->goods_desc; ?></textarea>
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label"><em>* </em>wap商品详情</label>
                            <div class="controls">
                                <textarea name="wap_goods_desc" rows="3" class="textarea-multipart-edit m-wrap span8 required"><?php echo $result->wap_goods_desc; ?></textarea>
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">商品品牌</label>
                            <div class="controls">
                                <select name="brand_id" class="m-wrap span8 chosen">
                                    <option value="0">请选择</option>
                                    <?php if ($brand->num_rows() > 0) : ?>
                                        <?php foreach ($brand->result() as $item) : ?>
                                            <option value="<?php echo $item->brand_id ?>" <?php if ($result->brand_id == $item->brand_id) : ?>selected="selected" <?php endif; ?>><?php echo $item->brand_name ?></option>
                                        <?php endforeach; ?>
                                    <?php endif; ?>
                                </select>
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label"><em>* </em>审核状态</label>
                            <div class="controls">
                                <select class="m-wrap span8 number required" name="is_check">
                                    <option value="1" <?php if ($result->is_check == 1): ?>selected="selected" <?php endif; ?>>未审核</option>
                                    <option value="2" <?php if ($result->is_check == 2): ?>selected="selected" <?php endif; ?>>审核通过</option>
                                    <option value="3" <?php if ($result->is_check == 3): ?>selected="selected" <?php endif; ?>>审核拒绝</option>
                                </select>
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label"><em>* </em>上下架</label>
                            <div class="controls">
                                <select class="m-wrap span8 required" name="is_on_sale">
                                    <option value="1" <?php if ($result->is_on_sale == 1): ?>selected="selected" <?php endif; ?>>上架</option>
                                    <option value="2" <?php if ($result->is_on_sale == 2): ?>selected="selected" <?php endif; ?>>下架</option>
                                </select>
                            </div>
                        </div>
                        <div class="control-group ">
                            <label class="control-label"><em>* </em>净重（g）</label>
                            <div class="controls">
                                <input type="text" name="weight" value="<?php echo $result->weight; ?>" class="m-wrap span8 required number" placeholder="重量(g)">
                            </div>
                        </div>
                        <div class="control-group ">
                            <label class="control-label"><em>* </em>限购数量(0为不限)</label>
                            <div class="controls">
                                <input type="text" name="limit_num" value="<?php echo $result->limit_num; ?>" class="m-wrap span8 required digits" placeholder="限购数量(0为不限)">
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">是否特产</label>
                            <div class="controls">
                                <label class="radio">
                                    <input type="radio" value="2" name="goods_native" <?php if ($result->goods_native === '2') :?>checked="checked"<?php endif;?>/>是
                                </label>
                                <label class="radio">
                                    <input type="radio" value="1" name="goods_native" <?php if ($result->goods_native === '1') :?>checked="checked"<?php endif;?>/>否
                                </label>
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">着想自营</label>
                            <div class="controls">
                                <label class="radio">
                                    <input type="radio" value="1" name="is_beizhu" <?php if ($result->is_beizhu === '1') :?>checked="checked"<?php endif;?>/>是
                                </label>
                                <label class="radio">
                                    <input type="radio" value="2" name="is_beizhu" <?php if ($result->is_beizhu === '2') :?>checked="checked"<?php endif;?>/>否
                                </label>
                            </div>
                        </div>
                        <div class="alert alert-success">商品销售信息</div>
                        <div class="control-group ">
                            <label class="control-label">市场价格</label>
                            <div class="controls">
                                <input type="text" name="market_price" value="<?php echo $result->market_price; ?>" class="m-wrap span8 number" placeholder="市场价格">
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label"><em>* </em> 着想价格</label>
                            <div class="controls">
                                <input type="text" name="shop_price" value="<?php echo $result->shop_price; ?>" class="m-wrap span8 required number" placeholder="着想价格">
                            </div>
                        </div>
                        <div class="control-group ">
                            <label class="control-label"><em>* </em> 供应价</label>
                            <div class="controls">
                                <input type="text" name="supply_price" value="<?php echo $result->supply_price; ?>" class="m-wrap span8 required number" placeholder="供应价">
                            </div>
                        </div>
                        <div class="control-group ">
                            <label class="control-label"><em>* </em>库存</label>
                            <div class="controls">
                                <input type="text" name="goods_num" value="<?php echo $result->goods_num; ?>" class="m-wrap span8 required number" placeholder="库存">
                            </div>
                        </div>
                        <div class="control-group ">
                            <label class="control-label"><em>* </em>已售数量</label>
                            <div class="controls">
                                <input type="text" name="sale_count" value="<?php echo $result->sale_count;?>" class="m-wrap span8 required number" placeholder="库存">
                            </div>
                        </div>
                        <div class="control-group ">
                            <label class="control-label"> 可用游币</label>
                            <div class="controls">
                                <input type="text" name="swim" value="<?php echo $result->swim; ?>" class="m-wrap span8 number" placeholder="可用游币">
                            </div>
                        </div>
                        <div class="alert alert-success">商品分润</div>
                        <div class="row-fluid add-supplieruid-html">
                            <div class="span12 pricing pricing-active hover-effect">
                                <div class="pricing-head pricing-head-active">
                                    <h3>商品价格分润</h3>
                                </div>
                                <ul class="pricing-content unstyled">
                                    <?php if (!empty($profit)): ?>
                                        <?php foreach ($profit->result() as $row): ?>
                                            <li>
                                                <i class="icon-tags"></i>
                                                <select name="type_id[]" class="m-wrap span2">
                                                    <?php foreach ($userType as $value): ?>
                                                        <option value="<?php echo $value->type_id; ?>" <?php if ($value->type_id == $row->bit_trade) echo 'selected="selected"'; ?>> <?php echo $value->type_name; ?></option>
                                                    <?php endforeach; ?>
                                                </select>
                                                <input type="text" name="uid[]" value="<?php echo $row->uid; ?>" class="m-wrap span4 supplieruid"/>
                                                <span>金额 </span><input type="text" name="amount[]" value="<?php echo $row->amount; ?>" class="m-wrap span4">
                                                <a href="javascript:void(0);" class="btn mini profit_delete">删除</a>
                                            </li>
                                        <?php endforeach; ?>
                                    <?php endif; ?>
                                </ul>
                                <div class="pricing-footer">
                                    <a id="profit_add" class="btn green" href="javascript:void(0);">添加分润人 <i class="m-icon-swapright m-icon-white "></i></a>
                                </div>
                            </div>
                        </div>
                        <?php if ($attribute->num_rows() > 0) :?>
                            <?php $attribute_key = trim($result->attribute_key, ',') ?>
                            <?php $attribute_key = explode(',', $attribute_key) ?>
                            <div class="alert alert-success ">商品属性</div>
                            <?php foreach ($attribute->result() as $key => $item) : ?>
                                <div class="control-group Father_Title">
                                    <div class="Father_Item">
                                        <label class="control-label attr_name"><?php echo $item->attr_name ?>：</label>
                                        <?php $spec_attr = $this->tourism_goods_category_spec->getSpecByAttId($item->attr_id); ?>
                                        <?php if ($spec_attr->result()) : ?>
                                            <div class="controls">
                                                <?php foreach ($spec_attr->result() as $val) : ?>
                                                    <label class="checkbox">
                                                        <input type="checkbox" name="attribute[]" value="<?php echo $val->spec_id ?>" <?php if (in_array($val->spec_id, $attribute_key)): ?>checked="checked" <?php endif; ?>/>
                                                        <?php echo $val->attribute_value ?>
                                                    </label>
                                                <?php endforeach; ?>
                                            </div>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        <?php endif;?>
                        <?php if ($additive->num_rows() > 0) :?>
                            <?php $goods_spec = unserialize($result->goods_spec) ?>
                            <div class="alert alert-success">商品附加属性</div>
                            <?php foreach ($additive->result() as $key => $item) : ?>
                                <div class="control-group ">
                                    <label class="control-label"><?php echo $item->name ?></label>
                                    <div class="controls">
                                        <input type="text" name="value[]" value="<?php echo isset($goods_spec[$item->name]) ? $goods_spec[$item->name] : '' ?>" class="m-wrap span8" >
                                        <input type="hidden" class="m-wrap large" name="key[]" value="<?php echo $item->name ?>">
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        <?php endif;?>
                        
                        <div class="alert alert-success">商品运费信息</div>
                        <div class="control-group">
                            <label class="control-label"><em>* </em>配送地址</label>
                            <div class="controls">
                                <?php $this->load->view('commonhtml/districtSelect'); ?>
                            </div>
                        </div>
                        <div class="control-group ">
                            <label class="control-label"> 详细地址</label>
                            <div class="controls">
                                <input type="text" name="detail" value="<?php echo trim(mb_strrchr($result->detail, ' ')); ?>" class="m-wrap span8" placeholder="用于根据地址搜索您的产品">
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">位置推荐</label>
                            <div class="controls">
                                <select name="show_flag" class="m-wrap span8">
                                    <option value="">请选择</option>
                                    <?php foreach ($show_flag as $key=>$value):?>
                                    <option value="<?php echo $key;?>" <?php if ($result->show_flag==$key):?>selected="selected"<?php endif;?>><?php echo $value;?></option>
                                    <?php endforeach;?>
                                </select>
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label"><em>* </em>支付方式</label>
                            <div class="controls">
                                <label class="checkbox">
                                    <input type="checkbox" class="required" value="1" name="payments[]" checked="checked"/>在线支付
                                </label>
                                <label class="checkbox">
                                    <input type="checkbox" class="required" value="2" name='payments[]'/>货到付款
                                </label>
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">运费</label>
                            <?php if ($template->num_rows() > 0): ?>
                            <div class="controls transport">
                                <label class="checkbox">
                                    <input type="radio" value="1" name="transport_type" <?php if ($result->transport_id != 0): ?>checked="checked"<?php endif; ?> data-key="<?php echo $result->transport_id ?>"/>
                                </label>
                                <select name="transport_id" class="required">
                                    <?php foreach ($template->result() as $item): ?>
                                        <option value="<?php echo $item->id ?>" <?php if ($item->id == $result->transport_id): ?>selected="selected"<?php endif; ?> ><?php echo $item->name ?></option>
                                    <?php endforeach; ?>
                                </select>
                                <label class="checkbox">使用运费模板</label>
                            </div>
                            <?php endif; ?>
                        </div>
                        <div class="control-group">
                            <label class="control-label"></label>
                            <div class="controls transport">
                                <label class="checkbox">
                                    <input type="radio" value="2" name="transport_type" <?php if ($result->transport_id == 0): ?>checked="checked"<?php endif; ?>/>
                                </label>
                                <input type="text" name="transport_cost" value="<?php echo $result->transport_cost ?>" id="transport_cost" class="required" <?php if ($result->transport_id != 0): ?>style="display: none"<?php endif; ?>/>
                                <label class="checkbox">自定义运费价格</label>
                            </div>
                        </div>
                        <div class="form-actions">
                            <button class="btn green" type="submit"><i class="icon-ok"></i> 保存</button>
                            <a class="btn" href="<?php echo base_url('tourismgoods/grid')?>">返回上一步</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<?php $this->load->view('layout/footer'); ?>
<script type="text/javascript">
$(document).ready(function(){
    $('.tourism-goods-add-step2').on("click", "input[name='transport_type']", function () {
        var obj = $(this).parents('label').next();
        var uid = $('input[name=uid]').val();
        var key = $(this).attr('data-key');
        if (uid == '') {
            alert('请先填写供应商');
        } else {
            if (obj[0].tagName == 'SELECT') {
                $('#transport_cost').hide();
            } else {
                $('#transport_id').hide();
            }
            obj.show();
            $.ajax({
                type: 'post',
                async: true,
                dataType: 'json',
                url: hostUrl() + '/tourismtemplet/ajaxGetTransport',
                data: {uid: uid},
                success: function (data) {
                    if (data) {
                        var transportObj = document.getElementById('transport_id');
                        var len = data.length;
                        transportObj.options.length = 0;
                        for (var i = 0; i < data.length; i++) {
                            transportObj.options[i] = new Option(data[i].name, data[i].id);
                            if (key == data[i].id) {
                                transportObj.selectedIndex = key;
                            }
                        }
                    }
                }
            });
        }
    });
    $('#user-responsive').on('click', 'table input[name=uid]', function(e){ //获取供应商uid的类别 --点击事件
        var uid = $(this).val();
        $.ajax({
              type  : 'post',
              async : false,
              dataType : 'json',
              url  : hostUrl() + '/tourismgoods/tourismProviderCategory',
              data : {provider_uid: uid},
              success : function(data){
                  if(data.status){
                	  if( $('.tourismgoods-form .provider-category').size() <= 0 ){
     	                 $('.category-list').after(data.content);
                      }
                  } else {
                      $('.provider-category').remove();
                  }
              }
        });
        e.preventDefault();
    });
    $('input[name="supplierUid"]').blur(function(e){ //获取供应商uid的类别 -- blur 事件
        var uid = $(this).val();
        $.ajax({
            type  : 'post',
            async : false,
            dataType : 'json',
            url  : hostUrl() + '/tourismgoods/tourismProviderCategory',
            data : {provider_uid: uid},
            success : function(data){
                if(data.status){
                    if( $('.tourismgoods-form .provider-category').size() <= 0 ) {
                   	   $('.category-list').after(data.content);
                    }
                } else {
                    $('.provider-category').remove();
                }
            }
        });
        e.preventDefault();
    });
    $("#profit_add").click(function () {
        var _val = '<li><i class="icon-tags"></i> <select name="type_id[]" class="m-wrap span2"><?php foreach ($userType as $value):?><option value="<?php echo $value->type_id;?>" > <?php echo $value->type_name;?></option><?php endforeach;?></select> <input class="m-wrap span3 number required supplieruid" type="text" name="uid[]" value="0"/> 金额 <input class="m-wrap span6 required number" type="text" name="amount[]" value="0"> <a href="javascript:void(0);" class="btn mini profit_delete">删除</a></li>';
        $(this).parent('.pricing-footer').prev().append(_val);
    });
    $('ul.pricing-content').on('click', ".profit_delete", function () {
        $(this).parent('li').remove();
    });

    // 提交验证
    $('form.tourismgoods-form').submit(function () {
        return false;
    }).validate({
        submitHandler: function (f) {
            $.ajax({
                type: 'post',
                async: true,
                dataType: 'json',
                beforeSend:function(){
                    $('button[type="submit"]').text('正在保存中').prop('disabled',true);
                },
                url: hostUrl() + '/tourismgoods/ajaxValidate',
                data: $('form.tourismgoods-form').serialize(),
                success: function (data) {
                    if (data.status) {
                        $('.alert-error').hide();
                        window.location.href = data.messages;
                    } else {
                        $('.alert-error').show();
                        $('.alert-error .remove_2 p').html(data.messages);
                        var body = (window.opera) ? (document.compatMode == 'CSS1Compat' ? $('html') : $('body')) : $('html,body');
                        body.animate({scrollTop: $('.page-container').offset().top}, 1000);
                        $('button[type="submit"]').text('请重新保存').prop('disabled',false);
                    }
                }
            });
        }
    });
});
</script>